/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controls;

import com.aptech.bluepumpkin.Registration;
import com.aptech.bluepumpkin.beans.RegistrationFacade;
import java.io.IOException;
import java.io.Serializable;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import tools.RegistrationDataModel;

/**
 *
 * @author Chien
 */
@ManagedBean
@SessionScoped
public class RegistrationControl implements Serializable {

    @EJB
    private RegistrationFacade registrationFacade;
    private Registration reg;
    private RegistrationDataModel dataModel;
    private Registration[] selectedRegs;

    public Registration[] getSelectedRegs() {
        return selectedRegs;
    }

    public void setSelectedRegs(Registration[] selectedRegs) {
        this.selectedRegs = selectedRegs;
    }

    public Registration getReg() {
        return reg;
    }

    public void setReg(Registration reg) {
        this.reg = reg;
    }

    public RegistrationDataModel getDataModel() {
        return dataModel;
    }

    public void setDataModel(RegistrationDataModel dataModel) {
        this.dataModel = dataModel;
    }

    public RegistrationDataModel buildAndGetDataMoel() {
        List<Registration> availableRegistration = registrationFacade.getAvailableRegistration();
        dataModel = new RegistrationDataModel(availableRegistration);
        return dataModel;
    }

    /**
     * Creates a new instance of RegistrationControl
     */
    public RegistrationControl() {
        reg = new Registration();
    }

    public List<Registration> getAvailableRegistrations() {
        return registrationFacade.getAvailableRegistration();
    }

    public List<Registration> getAllRegistration() {
        return registrationFacade.findAll();
    }

    public void delete(Registration reg) {
        try {
            registrationFacade.remove(reg);
            FacesContext context = FacesContext.getCurrentInstance();

            context.getExternalContext().redirect(context.getExternalContext().getRequestContextPath() + "/faces/Admin/RegManager.xhtml");
        } catch (IOException ex) {
            Logger.getLogger(RegistrationControl.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

   
    public void insert() {
        try {
            registrationFacade.create(reg);
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Create Success", ""));
        } catch (Exception ex) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Create Failed", ""));
        }
    }

    public void update() {
        try {
            registrationFacade.edit(reg);
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Update Success", ""));
        } catch (Exception ex) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Update Failed", ""));
        }
    }

    public void activate() {
        for (Registration registration : selectedRegs) {
            if (!registration.getActive()) {
                registration.setActive(Boolean.TRUE);
                registrationFacade.edit(registration);
            }
        }

    }
    public void deactivate() {
        for (Registration registration : selectedRegs) {
            if (registration.getActive()) {
                registration.setActive(Boolean.FALSE);
                registrationFacade.edit(registration);
            }
        }

    }
}
